#include "qfaultselecdialog.h"
#include "ui_qfaultselecdialog.h"

#include "../../libs/core/qsoftcore.h"
#include "../../libs/core/qfaultmanager.h"
#include "../../libs/core/qbaseitemdelegate.h"
#include "../../libs/core/stylehelper.h"

#include <QHeaderView>

QFaultSelecDialog::QFaultSelecDialog(QWidget *parent) :
    QBaseDialog(parent),
    ui(new Ui::QFaultSelecDialog),
    m_ret(0)
{
    ui->setupUi(this);

    ui->treeWidget->setItemDelegate(new QBaseItemDelegate);

    QList<tagFaultInfo*> list=QSoftCore::get_core()->fault_manager()->get_faults();

    m_unselect_icon=StyleHelper::drawCheckbox(this->palette(),this->style(),0);
    m_select_icon=StyleHelper::drawCheckbox(this->palette(),this->style(),1);

    tagFaultValue* value;
    foreach(tagFaultInfo *info,list)
    {
        QTreeWidgetItem *item=new QTreeWidgetItem(ui->treeWidget);

        item->setText(0,info->m_time);
        value=QSoftCore::get_core()->get_fault_value(info->m_code);

        if(value==NULL)
        {
            item->setText(1,QString::fromLocal8Bit("δ֪"));
            item->setText(2,QString::fromLocal8Bit("δ֪"));
        }
        else
        {
            item->setText(1,value->m_str_code);
            item->setText(2,value->m_name);
        }

        item->setIcon(0,m_unselect_icon);
        item->setData(0,Qt::UserRole,0);
    }

    ui->treeWidget->setAlternatingRowColors(true);
    ui->treeWidget->setRootIsDecorated(false);
    ui->treeWidget->header()->setMovable(false);

    QPalette p=ui->treeWidget->palette();
    p.setColor(QPalette::Inactive,QPalette::Highlight,p.color(QPalette::Active,QPalette::Highlight));
    p.setColor(QPalette::Inactive,QPalette::HighlightedText,p.color(QPalette::Active,QPalette::HighlightedText));
    ui->treeWidget->setPalette(p);

    ui->treeWidget->setColumnWidth(0,200);
    ui->treeWidget->setColumnWidth(1,200);

    connect(ui->treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(item_clicked(QTreeWidgetItem*)));
}

QFaultSelecDialog::~QFaultSelecDialog()
{
    delete ui;
}

void QFaultSelecDialog::on_pushButton_3_clicked()
{
    close();
}

void QFaultSelecDialog::on_pushButton_2_clicked()
{
    int count=ui->treeWidget->topLevelItemCount();
    for(int i=0;i<count;i++)
    {
        QTreeWidgetItem *item=ui->treeWidget->topLevelItem(i);
        item->setIcon(0,m_select_icon);
        item->setData(0,Qt::UserRole,1);
    }
}

void QFaultSelecDialog::on_pushButton_clicked()
{
    int count=ui->treeWidget->topLevelItemCount();
    for(int i=0;i<count;i++)
    {
        QTreeWidgetItem *item=ui->treeWidget->topLevelItem(i);
        if(item->data(0,Qt::UserRole).toInt()==0)
        {
            item->setIcon(0,m_select_icon);
            item->setData(0,Qt::UserRole,1);
        }
        else
        {
            item->setIcon(0,m_unselect_icon);
            item->setData(0,Qt::UserRole,0);
        }
    }
}

void QFaultSelecDialog::on_pushButton_4_clicked()
{
    QList<tagFaultInfo*> list1=QSoftCore::get_core()->fault_manager()->get_faults();
    QList<tagFaultInfo*> list2;
    int count=ui->treeWidget->topLevelItemCount();
    for(int i=0;i<count;i++)
    {
        QTreeWidgetItem *item=ui->treeWidget->topLevelItem(i);
        if(item->data(0,Qt::UserRole).toInt()==1)
        {
            list2.append(list1.at(i));
        }
    }

    foreach(tagFaultInfo* info,list2)
    {
        QSoftCore::get_core()->fault_manager()->remove_fault(info);
    }
    m_ret=1;
    close();
}

int QFaultSelecDialog::get_ret()
{
    return m_ret;
}

void QFaultSelecDialog::item_clicked(QTreeWidgetItem *item)
{
    if(item->data(0,Qt::UserRole).toInt()==0)
    {
        item->setIcon(0,m_select_icon);
        item->setData(0,Qt::UserRole,1);
    }
    else
    {
        item->setIcon(0,m_unselect_icon);
        item->setData(0,Qt::UserRole,0);
    }
}
